<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;

/**
 * 资料审核日志
 */
class ProfileVerifyLog extends Model
{
    protected $table = 'user_profile_verify_log';

    protected $fillable = ['status'];

    const STATUS_ING = 0;
    const STATUS_OK = 1;
    const STATUS_BAD = -1;
    const STATUS_NO = -2;
    const STATUS = [
        self::STATUS_ING => '待审核',
        self::STATUS_OK => '审核通过',
        self::STATUS_BAD => '审核不通过',
        self::STATUS_NO => '不可操作',
    ];

    const TYPE_SELF_INTRO = 1;
    const TYPE_NICKNAME = 2;
    const TYPE_VOICE = 3;
    const TYPE_POSTER = 4;
    const TYPE_QA = 5;
    const TYPE_AVATAR = 6;
    const TYPE_VIDEO = 7;
    const TYPE_REAL_AVATAR = 8;
    const TYPE_WECHAT = 9;
    const TYPE = [
        self::TYPE_SELF_INTRO => '爱情宣言',
        self::TYPE_NICKNAME => '昵称',
        self::TYPE_VOICE => '语音签名',
        self::TYPE_POSTER => '照片墙',
        self::TYPE_QA => '专访回答',
        self::TYPE_AVATAR => '头像',
        self::TYPE_VIDEO => '视频',
        self::TYPE_REAL_AVATAR => '真人头像认证',
        self::TYPE_WECHAT => '微信认证'
    ];

    public function userItem()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    public function userProfile()
    {
        return $this->belongsTo(UserProfile::class, 'user_id', 'user_id');
    }

    static function wechatHostIds()
    {
        $where = [
            ['content_type', self::TYPE_WECHAT],
            ['status', self::STATUS_OK]
        ];
        $hostIds = self::where($where)->pluck('user_id')->toArray();
        return array_values(array_unique($hostIds));
    }

    /**
     * 资料审核成功
     */
    static function verfyOk($record = null)
    {
        if ($record->status == self::STATUS_OK) return -1;
        $userId = $record->user_id;
        //修改状态
        $ret = self::where('id', $record->id)->limit(1)->update(['status' => self::STATUS_OK]);
        switch ($record->content_type) {
            case ProfileVerifyLog::TYPE_POSTER:
                $profile = UserProfile::where('user_id', $userId)->first();
                $oldPosters = $profile->poster ? explode(',', $profile->poster) : [];
                $newPosters = array_unique(array_merge($oldPosters, [$record->content]));
                // Log::info(__METHOD__ . "==Old:" . json_encode($oldPosters) . "==Content:{$record->content}==New:" . json_encode($newPosters));
                $profile->update(['poster' => implode(',', $newPosters)]);
                break;

            case ProfileVerifyLog::TYPE_VIDEO:
                $profile = UserProfile::where('user_id', $userId)->first();
                $profile->update(['video' => $record->sm_text]);
        }
        if ($record->content_type == ProfileVerifyLog::TYPE_WECHAT) {
            $msg = '恭喜你的微信认证资料已通过审核。';
            Message::pushSystem($userId, $msg, '认证通知');
        }
        return $ret;
    }

    /**
     * 资料审核失败
     */
    static function verfyBad($record = null)
    {
        if ($record->status == self::STATUS_BAD) return -1;
        $userId = $record->user_id;
        //修改状态
        $ret = self::where('id', $record->id)->limit(1)->update(['status' => self::STATUS_BAD]);
        //其他资料操作
        $profile = UserProfile::where('user_id', $userId)->first();
        switch ($record->content_type) {
            case ProfileVerifyLog::TYPE_POSTER:
                $oldPosters = $profile->poster ? explode(',', $profile->poster) : [];
                $newPosters = array_values(array_diff($oldPosters, [$record->content]));
                Log::info(__METHOD__ . "==Old:" . json_encode($oldPosters) . "==Content:{$record->content}==New:" . json_encode($newPosters));
                $profile->update(['poster' => implode(',', $newPosters)]);
                break;
            case ProfileVerifyLog::TYPE_VIDEO:
                if ($profile->video) {
                    $videoInfo = json_decode($profile->video, true);
                    Log::info(__METHOD__ . "==Video:{$videoInfo['link']}==Content:{$record->content}");
                    $videoInfo['link'] == $record->content && $profile->update(['video' => '']);
                }
                break;
            case ProfileVerifyLog::TYPE_NICKNAME:
                $profile->update(['nick_name' => '违规昵称']);
                break;
            case ProfileVerifyLog::TYPE_SELF_INTRO:
                $profile->update(['self_intro' => '']);
                break;
            case ProfileVerifyLog::TYPE_VOICE:
                $profile->update(['voice' => '']);
                $msg = '抱歉，您的语音介绍未能通过审核，请重新录制！';
                Message::pushSystem($userId, $msg, '语音审核通知');
                break;
                //专访
            case ProfileVerifyLog::TYPE_QA:
                $info = json_decode($record->content, true);
                if (isset($info['question_id']) && $info['question_id']) {
                    $where = ['user_id' => $userId, 'question_id' => $info['question_id']];
                    UserQa::where($where)->update(['flag' => 0]);
                }
                break;
            default:
        }

        if ($record->content_type == ProfileVerifyLog::TYPE_WECHAT) {
            $msg = '抱歉，您的微信认证资料未能通过审核，请更正账号信息后重新提交';
            Message::pushSystem($userId, $msg, '认证通知');
        }
        return $ret;
    }
}
